package com.jt.assistant.ui

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.afollestad.materialdialogs.list.listItems
import com.blankj.utilcode.util.ToastUtils
import com.jt.assistant.R
import com.jt.assistant.adapter.CarViewAdapter
import com.jt.assistant.base.GlobalConfig.JSON_RESULT_CAR_VIEW_ERROR
import com.jt.assistant.bean.CarView
import com.jt.assistant.bean.CityName
import com.jt.assistant.bean.Param
import com.jt.assistant.viewmodel.HomeViewModel
import com.yzp.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_car_view.*

/**
 * 落地车辆查看
 */
class CarViewActivity : BaseActivity<HomeViewModel>() {

    private val mAdapter by lazy {
        CarViewAdapter(
            ArrayList<CarView>()
        )
    }

    override fun onResume() {
        super.onResume()
        viewModel.getNoCheckWaybillCarList()
    }

    override fun layoutId(): Int = R.layout.activity_car_view

    override fun initView(savedInstanceState: Bundle?) {
        with(rv_carView) {
            layoutManager = LinearLayoutManager(context)
            adapter = mAdapter
        }
    }

    override fun initData() {

    }

    override fun initListener() {
        mAdapter.setOnItemClickListener { adapter, view, position ->
            startActivity(Intent(this@CarViewActivity, CarViewDetailActivity::class.java).apply {
                putExtra("Id", mAdapter.data[position].id)
            })

        }

        viewModel.mGetNoCheckWaybillCarList.observe(this, {
            if (it.isSuccess()) {
                if (it.data.carList.isNullOrEmpty()) ToastUtils.showShort("没有数据")
                mAdapter.data.clear()
                mAdapter.data.addAll(it.data.carList)
                mAdapter.notifyDataSetChanged()
            } else if (it.errorCode == JSON_RESULT_CAR_VIEW_ERROR) {
                viewModel.getCityNameList()
            }
        })

        viewModel.mRelationCity.observe(this, {
            if (it.isSuccess()) {
                ToastUtils.showShort(it.errorMsg)
                viewModel.getNoCheckWaybillCarList()
            }
        })

        viewModel.mGetCityNameList.observe(this, {
            if (it.isSuccess()) {
                showCityListDialog(it.data.cityNameList)
            }
        })
    }


    private fun showCityListDialog(cityNameList: List<CityName>) {//zhangcheng   zc123456
        val list = ArrayList<String>()
        cityNameList.forEach {
            list.add(it.cityName)
        }

        MaterialDialog(this).show {
            listItems(items = list) { _, index, text ->

                val params: Param = Param::class.java.newInstance()
                params.cityName = text.toString()
                log(text.toString())
                viewModel.relationCity(params)
            }
            lifecycleOwner(this@CarViewActivity)
        }
    }

}